home *** CD-ROM | disk | FTP | other *** search
/ PC Graphics Unleashed / PC Graphics Unleashed.iso / ch08 / icontest.c < prev    next >
C/C++ Source or Header  |  1994-09-15  |  5KB  |  214 lines

  1. // ICONTEST.C
  2. #include <conio.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <dos.h>
  6. #include <string.h>
  7. #include <graphics.h>
  8.  
  9. #define LMB 1
  10. #define RMB 2
  11.  
  12. typedef struct {
  13.   int x,y,width,height,color;
  14.   char str[81];
  15. } Icon;
  16.  
  17. int IsMouse(void);
  18. int MouseClick(void);
  19. void MouseXY(int *xpos,int *ypos);
  20. void MouseShow(int YesNo);
  21. void InitGraphics(void);
  22. Icon CreateIcon(int xo,int yo,int width,int height,
  23.         int color,char * text);
  24. void DrawIcon(Icon ic);
  25. int IconCheck(Icon ikon,int xm,int ym);
  26. void DrawIconHiLite(Icon ic, int OnOff);
  27.  
  28. union REGS inregs;
  29. union REGS outregs;
  30.  
  31. void main() {
  32.   int xpos, ypos, btn, i;
  33.   Icon icon[4];
  34.  
  35.   icon[0] = CreateIcon(100,100,70,15,RED  ," Icon 0");
  36.   icon[1] = CreateIcon(200,100,70,15,GREEN," Icon 1");
  37.   icon[2] = CreateIcon(300,100,70,15,BLUE ," Icon 2");
  38.   icon[3] = CreateIcon(200,130,70,15,LIGHTGRAY,"  Quit");
  39.  
  40.   InitGraphics();
  41.  
  42.   btn = IsMouse();
  43.   if(!btn) {
  44.       closegraph();
  45.       printf("Mouse Driver Not Installed!\n");
  46.       exit(1);
  47.   }
  48.  
  49.   /* Fill background with gray color */
  50.   setfillstyle(SOLID_FILL,LIGHTGRAY);
  51.   bar3d(0,0,getmaxx(),getmaxy(),0,0);
  52.  
  53.   DrawIcon(icon[0]);
  54.   DrawIcon(icon[1]);
  55.   DrawIcon(icon[2]);
  56.   DrawIcon(icon[3]);
  57.  
  58.   MouseShow(1);
  59.  
  60.   do {
  61.    do {
  62.      MouseXY(&xpos,&ypos);
  63.      btn = MouseClick();
  64.    }while(!btn);
  65.    if(btn == LMB)
  66.     for(i=0;i<4;i++) {
  67.      if(IconCheck(icon[i],xpos,ypos))
  68.        switch(i) {
  69.      case 0: DrawIconHiLite(icon[i], 1);
  70.          printf("\7");
  71.          DrawIconHiLite(icon[i], 0);
  72.          break;
  73.      case 1: DrawIconHiLite(icon[i], 1);
  74.          printf("\7");
  75.          DrawIconHiLite(icon[i], 0);
  76.          break;
  77.      case 2: DrawIconHiLite(icon[i], 1);
  78.          printf("\7");
  79.          DrawIconHiLite(icon[i], 0);
  80.          break;
  81.      case 3: btn = RMB; break;
  82.        }
  83.     }
  84.   }while(btn != RMB);
  85.  
  86.   closegraph();
  87. }
  88.  
  89. /* //////////////////////////////////////////////////*/
  90. void InitGraphics(void)
  91. {
  92.  int gdriver = DETECT, gmode, errorcode;
  93.  initgraph(&gdriver, &gmode, "");
  94.  if (gdriver != VGA) {
  95.   printf("VGA graphics card required.\n");
  96.   exit(1);
  97.  }
  98.  errorcode = graphresult();
  99.  if (errorcode != grOk)  /* an error occurred */
  100.  {
  101.    printf("Graphics error: %s\n", grapherrormsg(errorcode));
  102.    printf("Press any key to halt:");
  103.    getch();
  104.    exit(1); /* terminate with an error code */
  105.  }
  106. }
  107.  
  108. Icon CreateIcon(int xo,int yo,int width,int height,int color,char * text)
  109. {
  110.  Icon temp;
  111.  temp.x = xo;
  112.  temp.y = yo;
  113.  temp.width = width;
  114.  temp.height = height;
  115.  temp.color = color;
  116.  strcpy(temp.str,text);
  117.  return(temp);
  118. }
  119.  
  120. void DrawIcon(Icon ic)
  121. {
  122.  int color;
  123.  color = getcolor();
  124.  MouseShow(0);
  125.  setcolor(0);
  126.  setfillstyle(1,ic.color);
  127.  bar3d(ic.x,ic.y,ic.x+ic.width,ic.y+ic.height,0,0);
  128.  rectangle(ic.x,ic.y,ic.x+ic.width,ic.y+ic.height);
  129.  rectangle(ic.x+1,ic.y+1,ic.x+ic.width-1,ic.y+ic.height-1);
  130.  outtextxy(ic.x+3,ic.y + 5,ic.str);
  131.  setcolor(15);
  132.  line(ic.x,ic.y,ic.x+ic.width,ic.y);
  133.  line(ic.x,ic.y,ic.x,ic.y+ic.height);
  134.  line(ic.x+1,ic.y+1,ic.x+ic.width-1,ic.y+1);
  135.  line(ic.x+1,ic.y+1,ic.x+1,ic.y+ic.height-1);
  136.  outtextxy(ic.x+2,ic.y + 4,ic.str);
  137.  MouseShow(1);
  138.  setcolor(color);
  139. }
  140.  
  141. int IconCheck(Icon ikon,int xm,int ym)
  142. {
  143.  if ((xm > ikon.x) && (xm < ikon.x + ikon.width) &&
  144.      (ym > ikon.y) && (ym < ikon.y + ikon.height))
  145.   return(1);
  146.  else return(0);
  147. }
  148.  
  149.  
  150. void DrawIconHiLite(Icon ic, int OnOff)
  151. {
  152.  int color;
  153.  color = getcolor();
  154.  if (OnOff) {
  155.    MouseShow(0);
  156.    setcolor(WHITE);
  157.    rectangle(ic.x+1,ic.y+1,ic.x+ic.width-1,ic.y+ic.height-1);
  158.    setcolor(BLACK);
  159.    line(ic.x,ic.y,ic.x+ic.width,ic.y);
  160.    line(ic.x,ic.y,ic.x,ic.y+ic.height);
  161.    line(ic.x+1,ic.y+1,ic.x+ic.width-1,ic.y+1);
  162.    line(ic.x+1,ic.y+1,ic.x+1,ic.y+ic.height-1);
  163.    MouseShow(1);
  164.  }
  165.  else {
  166.    MouseShow(0);
  167.    setcolor(BLACK);
  168.    rectangle(ic.x+1,ic.y+1,ic.x+ic.width-1,ic.y+ic.height-1);
  169.    setcolor(WHITE);
  170.    line(ic.x,ic.y,ic.x+ic.width,ic.y);
  171.    line(ic.x,ic.y,ic.x,ic.y+ic.height);
  172.    line(ic.x+1,ic.y+1,ic.x+ic.width-1,ic.y+1);
  173.    line(ic.x+1,ic.y+1,ic.x+1,ic.y+ic.height-1);
  174.    MouseShow(1);
  175.    setcolor(color);
  176.  }
  177. }
  178.  
  179.  
  180. int IsMouse(void)
  181. {
  182.    inregs.x.ax = 0;
  183.    int86(0x33,&inregs,&outregs);
  184.    return(outregs.x.ax ? outregs.x.bx : 0);
  185. }
  186.  
  187. int MouseClick(void)
  188. {
  189.    int click = 0;
  190.    inregs.x.ax = 5;
  191.    inregs.x.bx = 1;
  192.    int86(0x33,&inregs,&outregs);
  193.    click = outregs.x.bx << 1;
  194.    inregs.x.bx--;
  195.    int86(0x33,&inregs,&outregs);
  196.    return(click | outregs.x.bx);
  197. }
  198.  
  199. void MouseXY(int *xpos,int *ypos)
  200. {
  201.    inregs.x.ax = 3;
  202.    int86(0x33,&inregs,&outregs);
  203.    *xpos = outregs.x.cx;
  204.    *ypos = outregs.x.dx;
  205. }
  206.  
  207.  
  208. void MouseShow(int YesNo)
  209. {
  210.    if (YesNo) inregs.x.ax = 1;
  211.    else inregs.x.ax = 2;
  212.    int86(0x33,&inregs,&outregs);
  213. }
  214.